<template>
  <!-- 轮播图 -->
  <div class="card-carousel-wrapper">
    <div
      class="card-carousel--nav__left"
      :disabled="atHeadOfList"
      @click="moveCarousel(-1)"
    ></div>
    <div class="card-carousel">
      <div class="card-carousel--overflow-container">
        <div
          class="card-carousel-cards"
          :style="{
            transform: 'translateX' + '(' + currentOffset + 'px' + ')'
          }"
        >
          <div
            v-for="(item, index) in items"
            :key="index"
            class="card-carousel--card"
            @click="$router.push(`/musiccenter/${item._id}`)"
          >
            <img :src="item.img" />
          </div>
        </div>
      </div>
    </div>

    <div
      class="card-carousel--nav__right"
      :disabled="atEndOfList"
      @click="moveCarousel(1)"
    ></div>
  </div>
</template>

<script>
export default {
  props: {
    items: { type: Array, required: true }
  },
  data() {
    return {
      currentOffset: 0,
      windowSize: 4,
      paginationFactor: 1000
    }
  },
  computed: {
    atEndOfList() {
      return (
        this.currentOffset <=
        this.paginationFactor * -1 * (this.items.length - this.windowSize)
      )
    },
    atHeadOfList() {
      return this.currentOffset === 0
    }
  },
  methods: {
    // 轮播图左右键
    moveCarousel(direction) {
      if (direction === 1 && !this.atEndOfList) {
        this.currentOffset -= this.paginationFactor
      } else if (direction === -1 && !this.atHeadOfList) {
        this.currentOffset += this.paginationFactor
      }
    }
  }
}
</script>

<style scoped>
.card-carousel-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 20px 0 40px;
  color: #666a73;
}
.card-carousel {
  display: flex;
  justify-content: center;
  width: 1000px;
}
.card-carousel--overflow-container {
  overflow: hidden;
}
.card-carousel--nav__left,
.card-carousel--nav__right {
  display: inline-block;
  width: 15px;
  height: 15px;
  padding: 10px;
  box-sizing: border-box;
  border-top: 2px solid #00000033;
  border-right: 2px solid #00000033;
  cursor: pointer;
  margin: 0 10px;
  transition: transform 150ms linear;
}

.card-carousel--nav__left:hover,
.card-carousel--nav__right:hover {
  border-top: 2px solid #1890ff;
  border-right: 2px solid #1890ff;
}
.card-carousel--nav__left[disabled],
.card-carousel--nav__right[disabled] {
  border-color: #f0f2f5;
}
.card-carousel--nav__left {
  transform: rotate(-135deg);
}
.card-carousel--nav__left:active {
  transform: rotate(-135deg) scale(0.9);
}
.card-carousel--nav__right {
  transform: rotate(45deg);
}
.card-carousel--nav__right:active {
  transform: rotate(45deg) scale(0.9);
}

.card-carousel-cards {
  display: flex;
  transition: transform 800ms ease-out;
  transform: translatex(0px);
}
.card-carousel-cards .card-carousel--card {
  margin: 0 10px;
  cursor: pointer;
  box-shadow: 0 4px 15px 0 rgba(40, 44, 53, 0.06),
    0 2px 2px 0 rgba(40, 44, 53, 0.08);
  background-color: #fff;
  border-radius: 4px;
  z-index: 3;
  margin-bottom: 2px;
}
.card-carousel-cards .card-carousel--card:first-child {
  margin-left: 0;
}
.card-carousel-cards .card-carousel--card:last-child {
  margin-right: 0;
}
.card-carousel-cards .card-carousel--card img {
  width: 480px;
  height: 190px;
  vertical-align: bottom;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  transition: opacity 150ms linear;
  user-select: none;
}
.card-carousel-cards .card-carousel--card img:hover {
  opacity: 0.5;
}
</style>
